import { request } from '../../assets/js/request.js';
import Toast from '@vant/weapp/toast/toast';
import Notify from '@vant/weapp/notify/notify';
var QQMapWX = require("../../assets/libs/qqmap-wx-jssdk.js");
var qqmapsdk;
Page({
  formSubmit:function(e){
    var dis = '';
    qqmapsdk.calculateDistance({
      mode: 'straight',
      from: this.data.signLocation,
      to: this.data.nowLocation,
      success: (res) => {
        var res = res.result;
        dis = res.elements[0].distance;
      },
      fail: function (error) {
        Notify({ type: 'danger', message: '请查看权限是否开启' });
      }
      
    });
    if(e.detail.value.signPwd == null){
      Notify({ type: 'danger', message: '请输入口令' });
    }else{
      if (this.data.nowLocation == '') {
        Notify({ type: 'danger', message: '请先获取当前所在位置' });
      } else if (dis > 30) {
        Notify({ type: 'danger', message: '签到失败，您当前的位置不在签到范围内' });
      } else {
        wx.showLoading({
          title: '请稍后',
          mask: true,
        })
        let reqData = {
          uid: wx.getStorageSync('uid'),
          classId: wx.getStorageSync('classId'),
          signPwd: e.detail.value.signPwd
        }
        request('/signNow.php', reqData)
          .then((res)=>{
            if (res.data == 'ok') {
              Toast({
                type: 'success',
                message: '签到成功',
                mask: true,
                duration: 1000
              })
            } else if (res.data == 'fail') {
              Toast({
                type: 'fail',
                message: '口令错误',
                mask: true,
                duration: 1000
              })
            } else if (res.data == 'repeat') {
              Toast({
                type: 'fail',
                message: '请勿重复签到',
                mask: true,
                duration: 1000
              })
            }
          })
          .catch((err)=>{
            Notify({ type: 'danger', message: '服务器繁忙，请稍后再试' });
          })
      }
    }
  },

  _getLocation: function(e){
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.userLocation']) {
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: (res) => {
              if (res.confirm) {
                wx.openSetting({
                  success: (res) => {

                  }
                })
              }
            }
          })
        } else {
          wx.showLoading({
            title: '正在打开地图',
            mask: true,
          })
          wx.getLocation({
            type: 'gcj02',
            isHighAccuracy: true,
            success: (res) => {
              const latitude = res.latitude
              const longitude = res.longitude
              this.setData({
                nowLocation: latitude + ',' + longitude
              })
              wx.openLocation({
                latitude: res.latitude,
                longitude: res.longitude,
              })
            },
            complete: (res) => {
              wx.hideLoading();
            }
          })
        }
      },
    })
    
  },

  data: {
    locationText:'',
    signLocation:'',
    nowLocation:''
  },

  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key: 'OGWBZ-CPCAW-XP3R4-RXVNM-N3X7V-NXBCN'
    });
    request('/getSignLocation.php', { classId: wx.getStorageSync('classId') })
      .then((res) => {
        this.setData({
          signLocation: res.data.signLocation
        })
      })
      .catch((err) => {
        Notify({ type: 'danger', message: '服务器繁忙' });
      })
  },
  
  onShow: function () {
    if (this.data.nowLocation != '') {
      this.setData({
        locationText: '位置已获取'
      })
    } else {
      this.setData({
        locationText: '点击获取当前位置'
      })
    }
  },
})